package cn.iocoder.yudao.module.trade.statistics.dal.mysql;

import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.module.trade.statistics.bo.TradeSummaryRespBO;
import cn.iocoder.yudao.module.trade.statistics.dal.dataobject.TradeStatisticsDO;
import cn.iocoder.yudao.module.trade.statistics.vo.TradeTrendSummaryRespVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 交易统计 Mapper
 *
 * @author owen
 */
@Mapper
public interface TradeStatisticsMapper extends CoreMapper<TradeStatisticsDO> {

    @Select("""
                    SELECT IFNULL(SUM(order_create_count), 0) AS count, IFNULL(SUM(order_pay_price), 0) AS summary
                    FROM trade_statistics
                    WHERE time BETWEEN #{beginTime} AND #{endTime} AND deleted = FALSE
            """)
    TradeSummaryRespBO selectOrderCreateCountSumAndOrderPayPriceSumByTimeBetween(@Param("beginTime") LocalDateTime beginTime,
                                                                                 @Param("endTime") LocalDateTime endTime);

    @Select("""
            SELECT
                -- 营业额 = 产品支付金额 + 充值金额
                SUM(order_pay_price + recharge_pay_price) AS turnoverPrice,
                SUM(order_pay_price) AS orderPayPrice,
                SUM(recharge_pay_price) AS rechargePrice,
                -- 支出金额 = 余额支付金额 + 支付佣金金额 + 产品退款金额
                SUM(wallet_pay_price + brokerage_settlement_price + after_sale_refund_price) AS expensePrice,
                SUM(wallet_pay_price) AS walletPayPrice,
                SUM(brokerage_settlement_price) AS brokerageSettlementPrice,
                SUM(after_sale_refund_price) AS afterSaleRefundPrice
            FROM trade_statistics
            WHERE time BETWEEN #{beginTime} AND #{endTime} AND deleted = FALSE
            """)
    TradeTrendSummaryRespVO selectVoByTimeBetween(@Param("beginTime") LocalDateTime beginTime,
                                                  @Param("endTime") LocalDateTime endTime);

    default List<TradeStatisticsDO> selectListByTimeBetween(LocalDateTime beginTime, LocalDateTime endTime) {
        return selectList(new CoreLambdaQueryWrapper<TradeStatisticsDO>()
                .between(TradeStatisticsDO::getTime, beginTime, endTime));
    }

    @Select("""
            <script>
                    SELECT -- 支出金额 = 余额支付金额 + 支付佣金金额 + 产品退款金额
                           SUM(wallet_pay_price + brokerage_settlement_price + after_sale_refund_price) AS expensePrice
                    FROM trade_statistics
                    WHERE deleted = FALSE
                    <if test="beginTime != null">
                        AND time <![CDATA[ >= ]]> #{beginTime}
                    </if>
                    <if test="endTime != null">
                        AND time <![CDATA[ <= ]]> #{endTime}
                    </if>
            </script>
            """)
    Integer selectExpensePriceByTimeBetween(@Param("beginTime") LocalDateTime beginTime,
                                            @Param("endTime") LocalDateTime endTime);

    default TradeStatisticsDO selectByTimeBetween(LocalDateTime beginTime, LocalDateTime endTime) {
        CoreLambdaQueryWrapper<TradeStatisticsDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.between(TradeStatisticsDO::getTime, beginTime, endTime);
        wrapper.limitN(1);
        return selectOne(wrapper);
    }

}
